/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package base;


import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.EmbeddedConfiguration;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import logicaDePredicados.LogicPredicate;
import logicaDePredicados.PredicateLogic;
import logicaDePredicados.SkolemFunction;
import predicatesAndQuerys.RetrieveAPredicateLogic;
import predicatesAndQuerys.RetrieveASkolemFunction;
import predicatesAndQuerys.RetrieveAllPredicateLogic;

/**
 *
 * @author Francisco Gindre
 */
public class KnowledgeBaseManager {

    /**
     * Returns the default configuration for this schema
     * @return
     */
    public static EmbeddedConfiguration getConfiguration(){
         EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
        config.common().objectClass(PredicateLogic.class).cascadeOnUpdate(true);
        config.common().objectClass(LogicPredicate.class).cascadeOnUpdate(true);
        config.common().objectClass(SkolemFunction.class).cascadeOnUpdate(true);

        config.common().objectClass(PredicateLogic.class).cascadeOnDelete(true);
        config.common().objectClass(LogicPredicate.class).cascadeOnDelete(true);
        config.common().objectClass(SkolemFunction.class).cascadeOnDelete(true);

        config.common().objectClass(PredicateLogic.class).updateDepth(1);
        config.common().objectClass(LogicPredicate.class).updateDepth(1);
        config.common().objectClass(SkolemFunction.class).updateDepth(2);
        return config;
    }

    /**
     * Gets the entire Knowledge base
     * @param knowledgeBaseFile
     * @return
     * @throws Exception
     */
    public static List<PredicateLogic> ObtainEntireKnowledgeBase(File knowledgeBaseFile) throws Exception {
        List<PredicateLogic> returnList = null;
        ObjectContainer db = null;
        try {
            db = Db4oEmbedded.openFile(getConfiguration(), knowledgeBaseFile.getAbsolutePath());
            ObjectSet<PredicateLogic> objectSet = db.query(new RetrieveAllPredicateLogic());
            returnList = new LinkedList<PredicateLogic>();

            for (PredicateLogic predicateLogic : objectSet) {
                returnList.add(predicateLogic);

            }
        } catch (Exception e) {
            throw e;

        } finally {
            if (db != null) {
               
                db.close();
            }
        }

        return returnList;
    }

    /**
     * * Stores a LogicPredicate element into de Database
     * @param element
     * @param knowledgeBaseFile
     * @throws Exception
     */
    public static void storeLogicPredicate(LogicPredicate<?> element, File knowledgeBaseFile) throws Exception {
      

        ObjectContainer db = null;
        try {

            db = Db4oEmbedded.openFile(getConfiguration(), knowledgeBaseFile.getAbsolutePath());
            db.store(element);
            db.commit();
        } catch (Exception e) {
            throw e;
        } finally {
            if (db != null) {
                db.rollback();
                db.close();
            }
        }


    }

    /**
     * Deletes a PredicateLogic from the Database
     * @param element
     * @param knowledgeBaseFile
     * @throws Exception
     */
    public static void deletePredicateLogicElement(PredicateLogic element, File knowledgeBaseFile) throws Exception {
        ObjectContainer db = null;

        try {

            db = Db4oEmbedded.openFile(getConfiguration(), knowledgeBaseFile.getAbsolutePath());
            ObjectSet<PredicateLogic> resultSet = db.query(new RetrieveAPredicateLogic(element));
            for (PredicateLogic predicateLogic : resultSet) {
                db.delete(predicateLogic);

            }


            db.commit();
            db.close();

        } catch (Exception e) {
            db.rollback();
            throw e;

        }


    }

    /**
     * Stores a SkolemFunction element into de Database
     * @param element
     * @param knowledgeBaseFile
     * @throws Exception
     */
    public static void storeSkolemFunction(SkolemFunction<?, ?> element, File knowledgeBaseFile) throws Exception {

        ObjectContainer db = null;
        try {

            db = Db4oEmbedded.openFile(getConfiguration(), knowledgeBaseFile.getAbsolutePath());
            db.store(element);
            db.commit();
            db.close();
        } catch (Exception e) {
            db.rollback();
            db.close();
            throw e;
        }



    }

    public static void deleteSkolemFunction(SkolemFunction<?, ?> element, File knowledgeBaseFile) throws Exception {

        ObjectContainer db = null;
        ObjectSet<SkolemFunction<?,?>> resultSet;
        try {

            db = Db4oEmbedded.openFile(getConfiguration(), knowledgeBaseFile.getAbsolutePath());
            resultSet =db.queryByExample(element);
            for (SkolemFunction<?, ?> skolemFunction : resultSet) {
                db.delete(skolemFunction);
            }
           
            db.commit();
            db.close();
        } catch (Exception e) {
            db.rollback();
            db.close();
            throw e;
        }



    }
}
